package com.infore.exchange.main.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.infore.common.base.EppPage;
import com.infore.common.base.Msg;
import com.infore.common.base.ResultUtil;
import com.infore.common.base.WrapperHelp;
import com.infore.common.fw.BaseController;
import com.infore.common.model.entity.exchange.TSourceDataInfo;
import com.infore.common.model.entity.vo.exchange.TSourceDataInfoVo;
import com.infore.exchange.main.service.TSourceDataInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 数据同步-数据来源 前端控制器
 * </p>
 *
 * @author lijh
 * @since 2023-02-10
 */
@RestController
@Api(value = "数据同步-数据来源前端控制器", tags = "数据同步-数据来源前端控制器")
@RequestMapping("/tSourceDataInfo")
public class TSourceDataInfoController extends BaseController {
	private String prefix = "/tSourceDataInfo";

	@Autowired
	private TSourceDataInfoService TSourceDataInfoService;

	@GetMapping()
	public String TSourceDataInfoController(){
		return prefix;
	}

	/**
	 * 查询数据同步-数据来源列表
	 * @param tsourcedatainfoVo 数据同步-数据来源的vo类
	 * @return Msg<Object> 结果集
	 * @author lijh
	 * @since 2023-02-10
	 */
	@PostMapping("/list")
	@ResponseBody
	@ApiOperation(value = "查询数据同步-数据来源列表", notes = "数据同步-数据来源列表查询 ")
	public Msg<Object> list(TSourceDataInfoVo tsourcedatainfoVo){
		//sql查询条件
		Wrapper<TSourceDataInfo> wrapper = null ;
		try {
			wrapper = (Wrapper<TSourceDataInfo>) WrapperHelp.addCondition(new EntityWrapper<TSourceDataInfo>(), tsourcedatainfoVo);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error(500,e.getMessage());
		}
		//查询方法
		Page<TSourceDataInfo> eppPage = new EppPage<TSourceDataInfo>(TSourceDataInfoService,tsourcedatainfoVo,wrapper);
		return ResultUtil.successPage(TSourceDataInfoService.selectPage(eppPage, wrapper));
	}

	/**
	 * 查询数据同步-数据来源列表
	 * @param tsourcedatainfo 数据同步-数据来源
	 * @return Msg<Object> 结果集
	 * @author lijh
	 * @since 2023-02-10
	 */
	@ResponseBody
	@ApiOperation(value = "添加数据同步-数据来源", notes = "数据同步-数据来源新增")
	@RequestMapping(value="/add",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	public Msg<Object> add(@RequestBody TSourceDataInfo tsourcedatainfo){
		Wrapper<TSourceDataInfo> wrapper = new  EntityWrapper<TSourceDataInfo>();
	    wrapper.eq("1", "1");
		List<TSourceDataInfo> entityList = TSourceDataInfoService.selectList(wrapper);
		if(entityList.size()>0) {
			return ResultUtil.error("重复添加");
		}
	//插入数据
		boolean flag = TSourceDataInfoService.insert(tsourcedatainfo);
		return ResultUtil.setResult(flag,tsourcedatainfo.getSourceId().toString());
	}


	/**
	  * 更新数据同步-数据来源方法
	  *
	  * @param tsourcedatainfo 数据同步-数据来源
	  * @return Msg<Object>
	  * @author lijh
	  * @since 2023-02-10
	  */
	@ResponseBody
	@ApiOperation(value = "更新数据同步-数据来源", notes = "数据同步-数据来源更新")
	@RequestMapping(value = "/edit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public Msg<Object> edit(@RequestBody TSourceDataInfo tsourcedatainfo) {
		//插入数据
		boolean flag = TSourceDataInfoService.updateById(tsourcedatainfo);
		return ResultUtil.setResult(flag, tsourcedatainfo.getSourceId().toString());
	}

	/**
	 * 删除数据同步-数据来源
	 *
	 * @param  sourceDataId
	 * @return Msg<Object>
	 * @author lijh
	 * @since 2023-02-10
	 */
	@RequestMapping(value = "/del", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ApiOperation(value = "删除数据同步-数据来源", notes = "删除数据同步-数据来源")
	public Msg<Object> del(String sourceDataId) {

		Map<String, Object> columnMap = new HashMap<String, Object>();
		columnMap.put("source_id", sourceDataId);
		boolean flag = TSourceDataInfoService.deleteByMap(columnMap);
		return ResultUtil.setResult(flag);
	}
}
